package com.unknown.wificontrol.ui.home;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.ui.BaseFragment;
import com.unknown.wificontrol.util.Constant;
import com.unknown.wificontrol.util.Preference;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment {


    @Override
    public int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initViews(View parent) {
        String ipAddr = Preference.get(mActivity, Constant.TAG_IP, Constant.defaultIP);
        tvIPAddress.setText(ipAddr);
    }

    @OnClick({R.id.llIncentive, R.id.llFilter, R.id.llGain, R.id.llCustom})
    void onItem(View v) {
        switch (v.getId()) {
            case R.id.llIncentive:
                changeView(IncentiveSettingActivity.class);
                break;
            case R.id.llFilter:
                changeView(FilterSettingActivity.class);
                break;
            case R.id.llGain:
                changeView(GainSettingActivity.class);
                break;
            case R.id.llCustom:
                changeView(CustomizeSettingActivity.class);
                break;
        }
    }

    @BindView(R.id.tvIPAddress)
    TextView tvIPAddress;

    @OnClick(R.id.llIPAddress)
    void onSetIPAddress() {
        String ip = Preference.get(mActivity, Constant.TAG_IP, Constant.defaultIP);
        final EditText inputServer = new EditText(mActivity);
        inputServer.setFocusable(true);
        inputServer.setText(ip);
        inputServer.setSelection(ip.length());
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        builder.setTitle("设置IP地址").setView(inputServer).setNegativeButton(
                "取消", null);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        String ipAddr = inputServer.getText().toString();
                        if (!TextUtils.isEmpty(ipAddr)) {
                            Preference.save(mActivity, Constant.TAG_IP, ipAddr);
                            tvIPAddress.setText(ipAddr);
                            showToast("设置成功");
                        }
                    }
                });
        builder.setCancelable(false);
        builder.show();
    }
}